#pragma once
#include "../../../src/server.hpp"
#include <iostream>
#include <string>
#include <cstring>


int main()
{
    Socket cli_sock;
    // 连接服务器（若连接失败，直接退出）
    if (!cli_sock.CreateClient("127.0.0.1", 8888)) {
        std::cerr << "Failed to connect to server!" << std::endl;
        return -1;
    }

    std::string user_input;
    while (true) {
        // 提示用户输入
        std::cout << "Enter message (type 'quit' to exit): ";
        std::getline(std::cin, user_input);

        // 输入 "quit" 时，退出循环并关闭连接
        if (user_input == "quit") {
            break;
        }

        // 发送用户输入到服务器
        cli_sock.Send(user_input.c_str(), user_input.size());

        // 接收服务器回显
        char recv_buf[1024] = {0};
        int recv_len = cli_sock.Recv(recv_buf, 1023);
        if (recv_len > 0) {
            std::cout << "Server echo: " << recv_buf << std::endl;
        } else {
            std::cerr << "Failed to receive echo (server may have closed connection)." << std::endl;
            break; // 接收失败或连接已关，提前退出循环
        }
    }

    // 关闭客户端套接字
    cli_sock.Close();
    std::cout << "Client connection closed." << std::endl;

    return 0;
}